$NG.AllReady(function (
	page,
	{ useValuesChange, useDataIndexChange, useUpdateRows, useUpdateRow }
) {
	const mstform = $NG.getCmpApi("p_form_visa_f_regist_m");
	const dgrid = $NG.getCmpApi("p_form_visa_f_regist_d");
	if (page.oType == "add" || page.oType == "edit") {
	}
	getInDate();
	function getInDate() {
		$NG.execServer("getInDate", {}, (res) => {
			if (res.count == 0) return;
			const _data = JSON.parse(res.data)[0].extendObjects;
			$NG.updateState((updater) => {
				updater.data.p_form_visa_f_regist_m.setProps({
					u_cycle: {
						label: _data.bill_name,
						value: _data.phid,
					},
				});
			});
		});
	}
    function sumAmt(){
        const row = dgrid.getRows()
        let ss_amt = row.reduce((value,item)=>{
            value+=(Number(item.u_ssje) || 0)
            return value
        },0)
        let sd_amt = row.reduce((value,item)=>{
            value+=(Number(item.u_sdje) || 0)
            return value
        },0)
        $NG.updateState((updater)=>{
            updater.data.p_form_visa_f_regist_m.setProps({
                u_sszje:ss_amt,
                u_sdzje:sd_amt
            })
        })
    }
    useValuesChange(({args})=>{
        getCntInfo()
    },"p_form_visa_f_regist_m.u_cnt")
    useValuesChange(({args})=>{
        cntMFilter()
        cntDFilter()
        getCntCount("p_form_visa_f_regist_m","u_cnt",()=>{
            getCntInfo()
        })
    },"p_form_visa_f_regist_m.phid_pc")
    function getCntInfo(){
        $NG.execServer(
            "getCntMInfoByPhid",
            { phid: mstform.getItem("u_cnt").getValue() },
            function (res) {
                const datas = JSON.parse(res.data)[0].extendObjects;
                console.log(`合同：`,datas);
                //帮助字段赋值
                $NG.updateState((updater) => {
                    updater.data.p_form_visa_f_regist_m.setProps({
                        u_cnt_no:datas.pcm_no
                    });
                });
            }
        );
    }
    cntMFilter()
    function cntMFilter() {
		if (!mstform.getItem("phid_pc").getValue()) {
			return;
		}
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_visa_f_regist_m.u_cnt.setProps({
				onBeforeOpen: async () => {
					var phid_pc = $NG
						.getCmpApi("p_form_visa_f_regist_m")
						.getItem("phid_pc")
						.getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!$NG
					.getCmpApi("p_form_visa_f_regist_m")
					.getItem("phid_pc")
					.getValue() && {
					clientSqlFilter: {
						bill_type: 1,
						phid_pc: $NG
							.getCmpApi("p_form_visa_f_regist_m")
							.getItem("phid_pc")
							.getValue(),
					},
				}),
			});
		});
	}
	useUpdateRows(({ args, table }) => {

        sumAmt()
	}, "p_form_visa_f_regist_d");
    cntDFilter()
    function cntDFilter(){
        if (mstform.getItem("phid_pc").getValue()) {
			$NG.updateUI((updater, state) => {
				updater.editGrid.p_form_visa_f_regist_d.u_cnt_name.setProps((e) => ({
					...e,
					editor: {
						...e.editor,
						required: true,
						clientSqlFilter: {
							phid_pc: mstform.getItem("phid_pc").getValue(),
							bill_type: 1,
						},
					},
				}));
			});
		} else {
			if (dgrid.getRows().length > 0&&false) {
				$NG.alert("请先选择项目");
				dgrid.clearRows(); //先清空明细
			}
		}
    }
	useDataIndexChange(({ args, table }) => {
		let row = args[0];
		console.log(row);
		$NG.execServer(
			"getCntMInfoByPhid",
			{ phid: row.u_cnt_name },
			function (res) {
				const datas = JSON.parse(res.data)[0].extendObjects; //单返回值
				console.log(datas);
				row.u_cnt_no = datas.pcm_no;
				row.u_htlx = datas.pct_id;
				row.u_htlx_EXName = datas.pct_name;
				dgrid.refreshView();
			}
		);
	}, "u_cnt_name");

    //获取合同数量
    getCntCount("p_form_visa_f_regist_m","u_cnt",()=>{
        getCntInfo()
    })
function getCntCount(table, key, fun, otherFun) {
    if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
    $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
      console.log(res, "count");
      switch (res.count) {
        case 0:
          return;
        case 1:
          const datas = JSON.parse(res.data)[0].extendObjects;
          const temp_map = {};
          temp_map[key] = datas.phid;
          temp_map[`${key}_EXName`] = datas.bill_name;
          $NG.updateState((updater) => {
            updater.data[table].setProps({
              ...temp_map,
            });
            setTimeout(() => {
              fun();
              if (otherFun) {
                otherFun();
              }
            }, 200);
          });
      }
    })
}
});
